package com.qfedu.og.service.impl;

import com.qfedu.og.dao.CompanyDao;
import com.qfedu.og.dao.GroupDao;
import com.qfedu.og.dto.GroupDto;
import com.qfedu.og.entity.Company;
import com.qfedu.og.entity.Group;
import com.qfedu.og.service.intf.CompanyService;
import com.qfedu.og.service.intf.GroupService;
import com.qfedu.og.util.RandomGroupUtil;
import com.qfedu.og.util.StringUtil;
import com.qfedu.og.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.image.ImageWatched;

import java.util.*;


/**
 * @program: OnlyGroup
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-03-25 14:46
 */
@Service //Spring IOC 反转
public class GroupServiceImpl implements GroupService {
    @Autowired //自动注入 前提 当前类的对象
    private GroupDao dao;

    @Override
    public R save(Group group) {
       if(dao.insert(group)>0){
           return new R(0,"OK",null);
       }else {
           return new R(1,"FAIL",null);
       }
    }

    @Override
    public R all() {
        return new R(0, "OK", dao.all());
    }

    @Override
    public R group(GroupDto dto) {
        //校验
        if(dto!=null){
            if(dto.getNum()>0 && dto.getPersons()!=null){
                //切割所有的名字 ,
                dto.getPersons().replaceAll("，",",");
                String[] names=dto.getPersons().split(",");
                //校验切割的结果
                if(names!=null && names.length>0){
                    //集合 记录所分的组
                    List<Group> list=new ArrayList<>();
                    //总人数10个人 每组 7人---- 1.7 3
                    //分多组
                    Random random=new Random();
                    if(names.length>dto.getNum()*1.5){
                        //计算需要分几组
                        int gps=0;
                        if(names.length%dto.getNum()<dto.getNum()/2){
                            gps=names.length/dto.getNum();
                        }else{
                            gps=names.length/dto.getNum()+1;
                        }
                        //将姓名的数组 变为集合
                        List<String> nameList=new ArrayList<>(Arrays.asList(names));
                        //循环随机为每组分配人员
                        for(int i=0;i<gps;i++){
                            Group group=new Group();
                            group.setCid(dto.getCid());
                            group.setNo(i+1);
                            group.setNum(dto.getNum());
                            //分配人员
                            StringBuffer buffer=new StringBuffer();
                            //对应的人就是组长
                            int pl=random.nextInt(dto.getNum());
                            for(int j=0;j<dto.getNum();j++){
                                //最后一组不一定是整除
                                if(nameList.size()<=0){
                                    break;
                                }
                                int index=random.nextInt(nameList.size());
                                if(j==pl){
                                    //组长就是你
                                    group.setGleader(nameList.remove(index));
                                }else {
                                    //随机取一个名字 添加
                                    buffer.append(nameList.remove(index) + ",");
                                }
                            }
                            //移除最后一位,
                            buffer.deleteCharAt(buffer.length()-1);
                            group.setPersons(buffer.toString());
                            group.setNum(dto.getNum());
                            list.add(group);//添加到本次分组中
                        }
                        //多余的人还是
                        if(nameList.size()>0){
                            for(int m=0;m<nameList.size();m++){
                                //缺陷：每次多的人都是 从前往后排
                                Group group=list.get(m);
                                group.setPersons(group.getPersons()+","+nameList.get(m));
                                group.setNum(dto.getNum()+1);

                            }
                        }
                    }else {
                        //就只有一组
                        Group group=new Group();
                        group.setCid(dto.getCid());
                        group.setNo(1);
                        group.setNum(names.length);
                        //如果就一个人，那么本人就是组长
                        if(names.length==1){
                            group.setGleader(names[0]);
                        }else {
                            group.setGleader(names[random.nextInt(names.length)]);
                            //排除组长 剩下来的人拼接
                            StringBuffer buffer = new StringBuffer();
                            for (String s : names) {
                                if (!s.equals(group.getGleader())) {
                                    buffer.append(s + ",");
                                }
                            }
                            buffer.deleteCharAt(buffer.length() - 1);
                            group.setPersons(buffer.toString());
                        }
                        list.add(group);
                    }
                    //Mybatis 批处理
                    if(dao.insertBatch(list)>0){
                        return new R(0,"OK",list);
                    }
                }
            }
        }
        return new R(1,"FAIL",null);
    }

    @Override
    public R randomGroup(GroupDto dto) {
        //校验
        if(dto!=null) {
            if (dto.getNum() > 0 && StringUtil.checkNotEmpty(dto.getPersons())) {
                List<Group> list = RandomGroupUtil.random(dto);
                if (dao.insertBatch(list) > 0) {
                    return new R(0, "OK", list);
                }
            }
        }
        return new R(1,"FAIL",null);
    }
}
